home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / gas / messages.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-23  |  10.6 KB  |  494 lines

  1. /* messages.c - error reporter -
  2.    Copyright (C) 1987, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GAS, the GNU Assembler.
  5.  
  6.    GAS is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    GAS is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with GAS; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  19.  
  20. #include "as.h"
  21.  
  22. #include <stdio.h>
  23. #ifdef HAVE_ERRNO_H
  24. #include <errno.h>
  25. #endif
  26.  
  27. #ifdef USE_STDARG
  28. #include <stdarg.h>
  29. #endif
  30.  
  31. #ifdef USE_VARARGS
  32. #include <varargs.h>
  33. #endif
  34.  
  35. #if !defined (USE_STDARG) && !defined (USE_VARARGS)
  36. /* Roll our own.  */
  37. #define va_alist REST
  38. #define va_dcl
  39. typedef int * va_list;
  40. #define va_start(ARGS)    ARGS = &REST
  41. #define va_end(ARGS)
  42. #endif
  43.  
  44. extern char *strerror ();
  45.  
  46. static void as_show_where PARAMS ((void));
  47. static void as_warn_internal PARAMS ((char *, unsigned int, char *));
  48. static void as_bad_internal PARAMS ((char *, unsigned int, char *));
  49.  
  50. /*
  51.  * Despite the rest of the comments in this file, (FIXME-SOON),
  52.  * here is the current scheme for error messages etc:
  53.  *
  54.  * as_fatal() is used when gas is quite confused and
  55.  * continuing the assembly is pointless.  In this case we
  56.  * exit immediately with error status.
  57.  *
  58.  * as_bad() is used to mark errors that result in what we
  59.  * presume to be a useless object file.  Say, we ignored
  60.  * something that might have been vital.  If we see any of
  61.  * these, assembly will continue to the end of the source,
  62.  * no object file will be produced, and we will terminate
  63.  * with error status.  The new option, -Z, tells us to
  64.  * produce an object file anyway but we still exit with
  65.  * error status.  The assumption here is that you don't want
  66.  * this object file but we could be wrong.
  67.  *
  68.  * as_warn() is used when we have an error from which we
  69.  * have a plausible error recovery.  eg, masking the top
  70.  * bits of a constant that is longer than will fit in the
  71.  * destination.  In this case we will continue to assemble
  72.  * the source, although we may have made a bad assumption,
  73.  * and we will produce an object file and return normal exit
  74.  * status (ie, no error).  The new option -X tells us to
  75.  * treat all as_warn() errors as as_bad() errors.  That is,
  76.  * no object file will be produced and we will exit with
  77.  * error status.  The idea here is that we don't kill an
  78.  * entire make because of an error that we knew how to
  79.  * correct.  On the other hand, sometimes you might want to
  80.  * stop the make at these points.
  81.  *
  82.  * as_tsktsk() is used when we see a minor error for which
  83.  * our error recovery action is almost certainly correct.
  84.  * In this case, we print a message and then assembly
  85.  * continues as though no error occurred.
  86.  */
  87.  
  88. static void
  89. identify (file)
  90.      char *file;
  91. {
  92.   static int identified;
  93.   if (identified)
  94.     return;
  95.   identified++;
  96.  
  97.   if (!file)
  98.     {
  99.       unsigned int x;
  100.       as_where (&file, &x);
  101.     }
  102.  
  103.   fprintf (stderr, "%s: Assembler messages:\n", file);
  104. }
  105.  
  106. static int warning_count;    /* Count of number of warnings issued */
  107.  
  108. int 
  109. had_warnings ()
  110. {
  111.   return (warning_count);
  112. }
  113.  
  114. /* Nonzero if we've hit a 'bad error', and should not write an obj file,
  115.    and exit with a nonzero error code */
  116.  
  117. static int error_count;
  118.  
  119. int 
  120. had_errors ()
  121. {
  122.   return (error_count);
  123. }
  124.  
  125.  
  126. /* Print the current location to stderr.  */
  127.  
  128. static void
  129. as_show_where ()
  130. {
  131.   char *file;
  132.   unsigned int line;
  133.  
  134.   as_where (&file, &line);
  135.   identify (file);
  136.   fprintf (stderr, "%s:%u: ", file, line);
  137. }
  138.  
  139. /*
  140.  *            a s _ p e r r o r
  141.  *
  142.  * Like perror(3), but with more info.
  143.  */
  144.  
  145. void 
  146. as_perror (gripe, filename)
  147.      const char *gripe;        /* Unpunctuated error theme. */
  148.      const char *filename;
  149. {
  150.   const char *errtxt;
  151.  
  152.   as_show_where ();
  153.   fprintf (stderr, gripe, filename);
  154. #ifdef BFD_ASSEMBLER
  155.   errtxt = bfd_errmsg (bfd_get_error ());
  156. #else
  157.   errtxt = strerror (errno);
  158. #endif
  159.   fprintf (stderr, ": %s\n", errtxt);
  160.   errno = 0;
  161. #ifdef BFD_ASSEMBLER
  162.   bfd_set_error (bfd_error_no_error);
  163. #endif
  164. }
  165.  
  166. /*
  167.  *            a s _ t s k t s k ()
  168.  *
  169.  * Send to stderr a string as a warning, and locate warning
  170.  * in input file(s).
  171.  * Please only use this for when we have some recovery action.
  172.  * Please explain in string (which may have '\n's) what recovery was done.
  173.  */
  174.  
  175. #ifdef USE_STDARG
  176. void 
  177. as_tsktsk (const char *format,...)
  178. {
  179.   va_list args;
  180.  
  181.   as_show_where ();
  182.   va_start (args, format);
  183.   vfprintf (stderr, format, args);
  184.   va_end (args);
  185.   (void) putc ('\n', stderr);
  186. }                /* as_tsktsk() */
  187. #else
  188. void 
  189. as_tsktsk (format, va_alist)
  190.      const char *format;
  191.      va_dcl
  192. {
  193.   va_list args;
  194.  
  195.   as_show_where ();
  196.   va_start (args);
  197.   vfprintf (stderr, format, args);
  198.   va_end (args);
  199.   (void) putc ('\n', stderr);
  200. }                /* as_tsktsk() */
  201. #endif /* not NO_STDARG */
  202.  
  203. /* The common portion of as_warn and as_warn_where.  */
  204.  
  205. static void
  206. as_warn_internal (file, line, buffer)
  207.      char *file;
  208.      unsigned int line;
  209.      char *buffer;
  210. {
  211.   ++warning_count;
  212.  
  213.   if (file == NULL)
  214.     as_where (&file, &line);
  215.  
  216.   identify (file);
  217.   fprintf (stderr, "%s:%u: Warning: ", file, line);
  218.   fputs (buffer, stderr);
  219.   (void) putc ('\n', stderr);
  220. #ifndef NO_LISTING
  221.   listing_warning (buffer);
  222. #endif
  223. }
  224.  
  225. /*
  226.  *            a s _ w a r n ()
  227.  *
  228.  * Send to stderr a string as a warning, and locate warning
  229.  * in input file(s).
  230.  * Please only use this for when we have some recovery action.
  231.  * Please explain in string (which may have '\n's) what recovery was done.
  232.  */
  233.  
  234. #ifdef USE_STDARG
  235. void 
  236. as_warn (const char *format,...)
  237. {
  238.   va_list args;
  239.   char buffer[200];
  240.  
  241.   if (!flag_no_warnings)
  242.     {
  243.       va_start (args, format);
  244.       vsprintf (buffer, format, args);
  245.       va_end (args);
  246.       as_warn_internal ((char *) NULL, 0, buffer);
  247.     }
  248. }                /* as_warn() */
  249. #else
  250. /*VARARGS1 */
  251. void 
  252. as_warn (format, va_alist)
  253.      const char *format;
  254.      va_dcl
  255. {
  256.   va_list args;
  257.   char buffer[200];
  258.  
  259.   if (!flag_no_warnings)
  260.     {
  261.       va_start (args);
  262.       vsprintf (buffer, format, args);
  263.       va_end (args);
  264.       as_warn_internal ((char *) NULL, 0, buffer);
  265.     }
  266. }                /* as_warn() */
  267. #endif /* not NO_STDARG */
  268.  
  269. /* as_warn_where, like as_bad but the file name and line number are
  270.    passed in.  Unfortunately, we have to repeat the function in order
  271.    to handle the varargs correctly and portably.  */
  272.  
  273. #ifdef USE_STDARG
  274. void 
  275. as_warn_where (char *file, unsigned int line, const char *format,...)
  276. {
  277.   va_list args;
  278.   char buffer[200];
  279.  
  280.   if (!flag_no_warnings)
  281.     {
  282.       va_start (args, format);
  283.       vsprintf (buffer, format, args);
  284.       va_end (args);
  285.       as_warn_internal (file, line, buffer);
  286.     }
  287. }                /* as_warn() */
  288. #else
  289. /*VARARGS1 */
  290. void 
  291. as_warn_where (file, line, format, va_alist)
  292.      char *file;
  293.      unsigned int line;
  294.      const char *format;
  295.      va_dcl
  296. {
  297.   va_list args;
  298.   char buffer[200];
  299.  
  300.   if (!flag_no_warnings)
  301.     {
  302.       va_start (args);
  303.       vsprintf (buffer, format, args);
  304.       va_end (args);
  305.       as_warn_internal (file, line, buffer);
  306.     }
  307. }                /* as_warn() */
  308. #endif /* not NO_STDARG */
  309.  
  310. /* The common portion of as_bad and as_bad_where.  */
  311.  
  312. static void
  313. as_bad_internal (file, line, buffer)
  314.      char *file;
  315.      unsigned int line;
  316.      char *buffer;
  317. {
  318.   ++error_count;
  319.  
  320.   if (file == NULL)
  321.     as_where (&file, &line);
  322.  
  323.   identify (file);
  324.   fprintf (stderr, "%s:%u: Error: ", file, line);
  325.   fputs (buffer, stderr);
  326.   (void) putc ('\n', stderr);
  327. #ifndef NO_LISTING
  328.   listing_error (buffer);
  329. #endif
  330. }
  331.  
  332. /*
  333.  *            a s _ b a d ()
  334.  *
  335.  * Send to stderr a string as a warning, and locate warning in input file(s).
  336.  * Please us when there is no recovery, but we want to continue processing
  337.  * but not produce an object file.
  338.  * Please explain in string (which may have '\n's) what recovery was done.
  339.  */
  340.  
  341. #ifdef USE_STDARG
  342. void 
  343. as_bad (const char *format,...)
  344. {
  345.   va_list args;
  346.   char buffer[200];
  347.  
  348.   va_start (args, format);
  349.   vsprintf (buffer, format, args);
  350.   va_end (args);
  351.  
  352.   as_bad_internal ((char *) NULL, 0, buffer);
  353. }
  354.  
  355. #else
  356. /*VARARGS1 */
  357. void 
  358. as_bad (format, va_alist)
  359.      const char *format;
  360.      va_dcl
  361. {
  362.   va_list args;
  363.   char buffer[200];
  364.  
  365.   va_start (args);
  366.   vsprintf (buffer, format, args);
  367.   va_end (args);
  368.  
  369.   as_bad_internal ((char *) NULL, 0, buffer);
  370. }
  371. #endif /* not NO_STDARG */
  372.  
  373. /* as_bad_where, like as_bad but the file name and line number are
  374.    passed in.  Unfortunately, we have to repeat the function in order
  375.    to handle the varargs correctly and portably.  */
  376.  
  377. #ifdef USE_STDARG
  378. void 
  379. as_bad_where (char *file, unsigned int line, const char *format,...)
  380. {
  381.   va_list args;
  382.   char buffer[200];
  383.  
  384.   va_start (args, format);
  385.   vsprintf (buffer, format, args);
  386.   va_end (args);
  387.  
  388.   as_bad_internal (file, line, buffer);
  389. }
  390.  
  391. #else
  392. /*VARARGS1 */
  393. void 
  394. as_bad_where (file, line, format, va_alist)
  395.      char *file;
  396.      unsigned int line;
  397.      const char *format;
  398.      va_dcl
  399. {
  400.   va_list args;
  401.   char buffer[200];
  402.  
  403.   va_start (args);
  404.   vsprintf (buffer, format, args);
  405.   va_end (args);
  406.  
  407.   as_bad_internal (file, line, buffer);
  408. }
  409. #endif /* not NO_STDARG */
  410.  
  411. /*
  412.  *            a s _ f a t a l ()
  413.  *
  414.  * Send to stderr a string as a fatal message, and print location of error in
  415.  * input file(s).
  416.  * Please only use this for when we DON'T have some recovery action.
  417.  * It exit()s with a warning status.
  418.  */
  419.  
  420. #ifdef USE_STDARG
  421. void 
  422. as_fatal (const char *format,...)
  423. {
  424.   va_list args;
  425.  
  426.   as_show_where ();
  427.   va_start (args, format);
  428.   fprintf (stderr, "Fatal error:");
  429.   vfprintf (stderr, format, args);
  430.   (void) putc ('\n', stderr);
  431.   va_end (args);
  432.   exit (EXIT_FAILURE);
  433. }                /* as_fatal() */
  434. #else
  435. /*VARARGS1*/
  436. void 
  437. as_fatal (format, va_alist)
  438.      char *format;
  439.      va_dcl
  440. {
  441.   va_list args;
  442.  
  443.   as_show_where ();
  444.   va_start (args);
  445.   fprintf (stderr, "Fatal error:");
  446.   vfprintf (stderr, format, args);
  447.   (void) putc ('\n', stderr);
  448.   va_end (args);
  449.   exit (EXIT_FAILURE);
  450. }                /* as_fatal() */
  451. #endif /* not NO_STDARG */
  452.  
  453. void
  454. fprint_value (file, val)
  455.      FILE *file;
  456.      valueT val;
  457. {
  458.   if (sizeof (val) <= sizeof (long))
  459.     {
  460.       fprintf (file, "%ld", val);
  461.       return;
  462.     }
  463. #ifdef BFD_ASSEMBLER
  464.   if (sizeof (val) <= sizeof (bfd_vma))
  465.     {
  466.       fprintf_vma (file, val);
  467.       return;
  468.     }
  469. #endif
  470.   abort ();
  471. }
  472.  
  473. void
  474. sprint_value (buf, val)
  475.      char *buf;
  476.      valueT val;
  477. {
  478.   if (sizeof (val) <= sizeof (long))
  479.     {
  480.       sprintf (buf, "%ld", val);
  481.       return;
  482.     }
  483. #ifdef BFD_ASSEMBLER
  484.   if (sizeof (val) <= sizeof (bfd_vma))
  485.     {
  486.       sprintf_vma (buf, val);
  487.       return;
  488.     }
  489. #endif
  490.   abort ();
  491. }
  492.  
  493. /* end of messages.c */
  494.